<template>
  <el-container>
    <el-main>
      <div class="default-main ba-table-box">
        <TableHeader
          :buttons="[
            'refresh',
            'add',
            'edit',
            'remove',
            'comSearch',
            'columnDisplay',
          ]"
          :quick-search-placeholder="placeholder"
          @action="baTable.onTableHeaderAction"
        />
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" @action="baTable.onTableAction" />
        <!-- 表单 -->
        <PopupForm v-if="baTable.form.operate" ref="formRef" />
      </div>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import { ref, provide, onMounted, markRaw, nextTick } from "vue";
import { useRouter, useRoute } from "vue-router";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import { ServiceEmploye } from "@/api/backend/index";
import useCurrentInstance from "@/utils/useCurrentInstance";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "./popupForm.vue";
import { useAdminInfo } from "@/store/adminInfo";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useStaff } from "@/store/backend/ServiceM/staff";
import { useNavTabs } from "@/store/navTabs";

const { proxy } = useCurrentInstance(); //全局事件总线
const tableRef = ref();
const placeholder = ref<string>("模糊搜索");
const adminInfo = useAdminInfo();
const router = useRouter();
const staff = useStaff();
const route = useRoute();
const navTabs = useNavTabs();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + ServiceEmploye)
      ? (navTabs as any).pageSizeMap.get(route.path + ServiceEmploye)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(ServiceEmploye),
  {
    pk: "id",
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("name"),
        label: "售后人员姓名",
        prop: "name",
        align: "center",
        operator: false,
      },
      {
        show: filedShowOff("phone"),
        label: "售后人员电话",
        prop: "phone",
        align: "center",
        operatorPlaceholder: "模糊搜索",
      },
      // {
      //   label: "隶属服务商",
      //   prop: "providerId",
      //   align: "center",
      //   operatorPlaceholder: "模糊搜索",
      // },
      {
        // show: operatorShowOff([route.path + "/remove", route.path + "/edit"]),
        label: "操作",
        align: "center",
        width: "130",
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "fa fa-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "confirmButton",
            name: "remove",
            title: "删除",
            text: "",
            type: "danger",
            icon: "el-icon-DeleteFilled",
            class: "table-row-delete",
            popconfirm: {
              confirmButtonText: "确认",
              cancelButtonText: "取消",
              confirmButtonType: "danger",
              title: "确认删除？",
            },
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "tipButton",
            name: "info",
            title: "详情",
            text: "",
            type: "primary",
            icon: "fa fa-search-plus",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return false;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
        operator: false,
      },
    ],
    filter: {
      pageSize: pageSizeKeep(),
    },
    acceptQuery: false,
  },
  {
    defaultItems: {}, //默认值
  },
  {}
);

//跳转售后人员详情页
const infoButtonClick = (name: string, row: any) => {
  console.log("row", row);
  staff.$patch((state) => {
    state.single = row;
  });
  router.push({
    path: "/admin/ServicesM/staffDetail",
    query: { employeeId: row.id },
  });
};

onMounted(() => {
  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(ServiceEmploye);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {
      if (!baTable.activate) return;
      if (data.name == "info") {
        //携带主键字段pk
        infoButtonClick(
          (data.row as TableRow)[baTable.table.pk!],
          data.row as any
        );
      }
    }
  );
});

// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("adminInfo", adminInfo);
</script>
<style lang="scss" scoped>
.el-main {
  padding: 0;
}
.default-main {
  margin: var(--ba-main-space) var(--ba-main-space) 60px var(--ba-main-space);
}
</style>
